//
//  TopicSquareTableViewCell.swift
//  HoneyBee
//
//  Created by point on 2018/1/24.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit

@objc protocol TopicSquareTableViewCellDelegate {
    func deleteTopic(topic: TopicModel, index: IndexPath)
    @objc optional func didLongPress(topic:TopicModel, index: IndexPath)
}

class TopicSquareTableViewCell: UITableViewCell {
    
    public weak var delegate: TopicSquareTableViewCellDelegate?
    
    @IBOutlet weak var topicDescritionLabel: UILabel!
    @IBOutlet weak var topicImageview: UIImageView!
    @IBOutlet weak var imageWidth: NSLayoutConstraint!
    @IBOutlet weak var topicAuthorLabel: UILabel!
    @IBOutlet weak var topicAuthorImageview: UIImageView!
    @IBOutlet weak var topicGroupLabel: UILabel!
    @IBOutlet weak var topicGroupImageview: UIImageView!
    @IBOutlet weak var topicCommentCountLabel: UILabel!
    @IBOutlet weak var topicCommentCountImageview: UIImageView!
    @IBOutlet weak var topicDeleteButton: UIButton!

    let longPress = UILongPressGestureRecognizer.init(target: self, action: #selector(didLongPress(longPress:)))
    
    @IBAction func topicDeleteAction(_ sender: Any) {
        guard let topic = model else {return}
        guard let i = index else {return}
        delegate?.deleteTopic(topic: topic, index: i)
    }
    
    
    var model:TopicModel?
    var index: IndexPath?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        topicAuthorImageview.image = UIImage.icon(from: kIcon_author, color: kColorTextPlacehold, imageSize: CGSize(width: 12, height: 12)).withRenderingMode(.alwaysOriginal)
        topicGroupImageview.image = UIImage.icon(from: kIcon_group, color: kColorTextPlacehold, imageSize: CGSize(width: 12, height: 12)).withRenderingMode(.alwaysOriginal)
        topicCommentCountImageview.image = UIImage.icon(from: kIcon_comments, color: kColorTextPlacehold, imageSize: CGSize(width: 12, height: 12)).withRenderingMode(.alwaysOriginal)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    func configModel(model:TopicModel , index:IndexPath) {
        self.model = model
        self.index = index
        
        topicCommentCountLabel.text = String(format: "%d", model.commentsNum)
        if model.groupName.isEmpty {
            topicGroupImageview.isHidden = true
        } else {
            topicGroupImageview.isHidden = false
        }
        topicGroupLabel.text = model.groupName
        
        if model.authorName.isEmpty {
            topicAuthorImageview.isHidden = true
        } else {
            topicAuthorImageview.isHidden = false
        }
        topicAuthorLabel.text = model.authorName
        if let url = URL(string: model.firstImg) {
            topicImageview.setImageWith(url, placeholderImage: nil)
            topicImageview.layer.cornerRadius = 3
            topicImageview.layer.masksToBounds = true
            imageWidth.constant = 100
        } else {
            imageWidth.constant = 0
        }
        topicDescritionLabel.text = model.title
        
    }
    
    
    @objc func didLongPress(longPress : UILongPressGestureRecognizer) {
        if longPress.state == .recognized {
            if delegate != nil {
                delegate?.didLongPress?(topic: model!, index: index!)
            }
        
        }
    }
}
